package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Domainmodelattr;
import sjtu.ist.warehouse4ws.model.Domainmodelstate;

public class ParseDomainModel {
	public String modelId;

	public String parseNode(String tagName, Document doc){
		
        NodeList nodelist= doc.getElementsByTagName(tagName);      //获取该ID的标签
     
        Element node1= (Element) nodelist.item(0);                 //获取该ID标签的第一个Item
       
        NodeList nodelist2=node1.getChildNodes();                 //获取该ID标签第一个Item里面的结点
		if (nodelist2.getLength() != 0) {
			String ttt = nodelist2.item(0).getNodeValue(); // 取值
			if (ttt.isEmpty()) {
				return null;
			} else {
				return ttt;
			}
		} else {
			return null;
		}
		 
	}
	public Domainmodel reserveModel(String path)  {
	//	String path="E:/Users/chenqidou/Desktop/models/371a5016-6d5e-4e9a-abcb-d7419733ac4c.xml";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Domainmodel domainModel = new Domainmodel();
		
		try {
			db = dbf.newDocumentBuilder();
			File file = new File(path);
			Document doc = db.parse(file);
			modelId=parseNode("id",doc);
			String enName=parseNode("enName",doc);
			String cnName=parseNode("cnName",doc);
			String description =parseNode("description",doc);
			String type=parseNode("type",doc);
			String category=parseNode("category",doc);
			domainModel.setId(modelId);
			domainModel.setEnName(enName);
			domainModel.setCnName(cnName);
			domainModel.setDescription(description);
			domainModel.setType(type);
			domainModel.setCategory(category);
			//domainModel.setFileinfo(null);
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return domainModel;
		
		
	}
	public ArrayList<Domainmodelattr> reserveDomianmodelattr(String path) {
	//	String path="E:/Users/chenqidou/Desktop/models/371a5016-6d5e-4e9a-abcb-d7419733ac4c.xml";
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		
		ArrayList<Domainmodelattr> list=new ArrayList<Domainmodelattr>();
			try {
				db = dbf.newDocumentBuilder();
				File file = new File(path);
				Document doc = null;
				doc = db.parse(file);
				Element root=doc.getDocumentElement();
				NodeList modelattrs=root.getElementsByTagName("domain_model_attribute");
				for(int i=0;i<modelattrs.getLength();i++){
					Domainmodelattr domainmodelattr=new Domainmodelattr();
					Element modelattr=(Element) modelattrs.item(i);
					NodeList idList=modelattr.getElementsByTagName("id");
					String id=getNodeText(idList);
					System.out.println(id);
					domainmodelattr.setId(id);
					
					NodeList enNameList=modelattr.getElementsByTagName("enName");
					String enName=getNodeText(enNameList);
					
					domainmodelattr.setEnName(enName);
					
					NodeList cnNameList=modelattr.getElementsByTagName("cnName");
					String cnName=getNodeText(cnNameList);
					System.out.println(cnName);
					domainmodelattr.setCnName(cnName);
					
					NodeList descriptionList=modelattr.getElementsByTagName("description");
					String description=getNodeText(descriptionList);
					domainmodelattr.setDescription(description);
					
					NodeList cnTypeList=modelattr.getElementsByTagName("cnType");
					String cnType=getNodeText(cnTypeList);
					domainmodelattr.setCnType(cnType);
					
					NodeList enTypeList=modelattr.getElementsByTagName("enType");
					String enType=getNodeText(enTypeList);
					domainmodelattr.setEnType(enType);
					
					NodeList typeIdList=modelattr.getElementsByTagName("typeId");
					String typeId=getNodeText(typeIdList);
					domainmodelattr.setTypeId(typeId);
					
					NodeList maxValueList=modelattr.getElementsByTagName("maxValue");
					String maxValue=getNodeText(maxValueList);
					domainmodelattr.setMaxValue(maxValue);
					
					NodeList minValueList=modelattr.getElementsByTagName("minValue");
					String minValue=getNodeText(minValueList);
					domainmodelattr.setMinValue(minValue);
					
					NodeList readOnlyList=modelattr.getElementsByTagName("readOnly");
					String readOnly=getNodeText(readOnlyList);
					domainmodelattr.setReadOnly(Boolean.valueOf(readOnly));
					
					
					NodeList staticStateList=modelattr.getElementsByTagName("staticState");
					String staticState=getNodeText(staticStateList);
					domainmodelattr.setStaticstate(Boolean.valueOf(staticState));
					
					NodeList uniqueList=modelattr.getElementsByTagName("unique");
					String unique=getNodeText(uniqueList);
					domainmodelattr.setUni(Boolean.valueOf(unique));
					
					NodeList collectionTypeList=modelattr.getElementsByTagName("collectionType");
					String collectionType=getNodeText(collectionTypeList);
					domainmodelattr.setCollectionType(collectionType);
					
					NodeList defaultValueList=modelattr.getElementsByTagName("defaultValue");
					String defaultValue=getNodeText(defaultValueList);
					domainmodelattr.setDefaultValue(defaultValue);	
					
					domainmodelattr.setDomainModelid(modelId);
				
					list.add(domainmodelattr);
			}
				} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				
				e.printStackTrace();
			} catch (IOException e) {
			
				e.printStackTrace();
			}
	
			return list;

	}
	public ArrayList<Domainmodelstate>reserveState(String path){
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		ArrayList<Domainmodelstate> list=new ArrayList<Domainmodelstate>();
		try {
			db = dbf.newDocumentBuilder();
			File file = new File(path);
			Document doc = null;
			doc = db.parse(file);
			Element root=doc.getDocumentElement();
			NodeList modelattrs=root.getElementsByTagName("domain_model_state");
			for(int i=0;i<modelattrs.getLength();i++){
				Domainmodelstate Domainmodelstate=new Domainmodelstate();
				Element modelattr=(Element) modelattrs.item(i);
				NodeList idList=modelattr.getElementsByTagName("id");
				String id=getNodeText(idList);
				System.out.println(id);
				Domainmodelstate.setId(id);
				
				NodeList enNameList=modelattr.getElementsByTagName("enName");
				String enName=getNodeText(enNameList);
				Domainmodelstate.setEnName(enName);
				
				NodeList cnNameList=modelattr.getElementsByTagName("cnName");
				String cnName=getNodeText(cnNameList);
				System.out.println(cnName);
				Domainmodelstate.setCnName(cnName);
				
				NodeList descriptionList=modelattr.getElementsByTagName("description");
				String description=getNodeText(descriptionList);
				Domainmodelstate.setDescription(description);

				Domainmodelstate.setDomainModelid(modelId);
			
				list.add(Domainmodelstate);
		}
			} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
		
			e.printStackTrace();
		}

		return list;
		
	}
	
	
	
	
	
	public String getNodeText(NodeList node){
		Element e=(Element)node.item(0);
		if(e==null){return null;}                        //this judge is very important ,without it ,you will found nullpoint Exception
		Text t=(Text)e.getFirstChild();
		if(t==null){return null;}
		String str=t.getNodeValue();
		return str;

	}
	
	public static void main(String[] args) throws IOException, Exception {
		
		ParseDomainModel hel=new ParseDomainModel();
		String path="E:/Users/chenqidou/Desktop/models/a1c38a8d-479e-4a44-ae95-024edda997c1.xml";
		hel.reserveModel(path);
		ArrayList<Domainmodelstate>hell=hel.reserveState(path);
		for(int i=0;i<hell.size();i++){
			System.out.println(hell.get(i).getDomainModelid());
			System.out.println(hell.get(i).getCnName());
			System.out.println(hell.get(i).getEnName());
		}
//		System.out.println(hel.reserveDomianmodelattr(path).get(0).getId());


	}

}
